package dataMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import dataEntity.MeasuresOfBirthAndDeath;

public class MeasuresOfBirthAndDeathMapper {
	public static  Map<String,Map<String,MeasuresOfBirthAndDeath>> process(BufferedReader br){
		Map<String,Map<String,MeasuresOfBirthAndDeath>> map = new HashMap<String,Map<String,MeasuresOfBirthAndDeath>> ();
		try {
			br.readLine();
			while(br.ready()){
				Iterable<CSVRecord> parser = CSVFormat.EXCEL.parse(br);
				for (CSVRecord record : parser) {
					MeasuresOfBirthAndDeath object = buildObject(record);

					Map<String,MeasuresOfBirthAndDeath> stateMap = map.get(object.getCHSI_State_Abbr());
					if(stateMap == null) stateMap = new HashMap<String,MeasuresOfBirthAndDeath>();
					stateMap.put(object.getCHSI_County_Name(), object);
					map.put(object.getCHSI_State_Abbr(), stateMap);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static void info(Map<String,Map<String,MeasuresOfBirthAndDeath>> map){
		TreeSet<Double> infoSet1 = new TreeSet<Double>();
		TreeSet<Double> infoSet2 = new TreeSet<Double>();
		TreeSet<Double> infoSet3 = new TreeSet<Double>();
		Set<String> stateSet = map.keySet();
		for(String s : stateSet){
			Map<String, MeasuresOfBirthAndDeath> countyMap = map.get(s);
			Set<String> countySet = countyMap.keySet();
			for(String countyString : countySet){
				MeasuresOfBirthAndDeath obj = countyMap.get(countyString);
				infoSet1.add(obj.getBrst_Cancer());
				infoSet2.add(obj.getCol_Cancer());
				infoSet3.add(obj.getLung_Cancer());
			}
		}
//		System.out.println("Breast Smallest " + infoSet1.first() + " Largest " + infoSet1.last());
//		System.out.println("Colon Smallest " + infoSet2.first() + " Largest " + infoSet2.last());
//		System.out.println("Lung Smallest " + infoSet3.first() + " Largest " + infoSet3.last());
	}
	
	public static  MeasuresOfBirthAndDeath buildObject(CSVRecord record){
		MeasuresOfBirthAndDeath object = new MeasuresOfBirthAndDeath();

		object.setState_FIPS_Code(record.get(0));
		object.setCounty_FIPS_Code(record.get(1));
		object.setCHSI_County_Name(record.get(2));
		object.setCHSI_State_Name(record.get(3));
		object.setCHSI_State_Abbr(record.get(4));
		object.setStrata_ID_Number(record.get(5));
		object.setLBW(record.get(6));
		object.setLBW_Ind(record.get(7));
		object.setMin_LBW(record.get(8));
		object.setMax_LBW(record.get(9));
		object.setCI_Min_LBW(record.get(10));
		object.setCI_Max_LBW(record.get(11));
		object.setVLBW(record.get(12));
		object.setVLBW_Ind(record.get(13));
		object.setMin_VLBW(record.get(14));
		object.setMax_VLBW(record.get(15));
		object.setCI_Min_VLBW(record.get(16));
		object.setCI_Max_VLBW(record.get(17));
		object.setPremature(record.get(18));
		object.setPremature_Ind(record.get(19));
		object.setMin_Premature(record.get(20));
		object.setMax_Premature(record.get(21));
		object.setCI_Min_Premature(record.get(22));
		object.setCI_Max_Premature(record.get(23));
		object.setUnder_18(record.get(24));
		object.setUnder_18_Ind(record.get(25));
		object.setMin_Under_18(record.get(26));
		object.setMax_Under_18(record.get(27));
		object.setCI_Min_Under_18(record.get(28));
		object.setCI_Max_Under_18(record.get(29));
		object.setOver_40(record.get(30));
		object.setOver_40_Ind(record.get(31));
		object.setMin_Over_40(record.get(32));
		object.setMax_Over_40(record.get(33));
		object.setCI_Min_Over_40(record.get(34));
		object.setCI_Max_Over_40(record.get(35));
		object.setUnmarried(record.get(36));
		object.setUnmarried_Ind(record.get(37));
		object.setMin_Unmarried(record.get(38));
		object.setMax_Unmarried(record.get(39));
		object.setCI_Min_Unmarried(record.get(40));
		object.setCI_Max_Unmarried(record.get(41));
		object.setLate_Care(record.get(42));
		object.setLate_Care_Ind(record.get(43));
		object.setMin_Late_Care(record.get(44));
		object.setMax_Late_Care(record.get(45));
		object.setCI_Min_Late_Care(record.get(46));
		object.setCI_Max_Late_Care(record.get(47));
		object.setInfant_Mortality(record.get(48));
		object.setInfant_Mortality_Ind(record.get(49));
		object.setMin_Infant_Mortality(record.get(50));
		object.setMax_Infant_Mortality(record.get(51));
		object.setCI_Min_Infant_Mortality(record.get(52));
		object.setCI_Max_Infant_Mortality(record.get(53));
		object.setIM_Wh_Non_Hisp(record.get(54));
		object.setIM_Wh_Non_Hisp_Ind(record.get(55));
		object.setMin_IM_Wh_Non_Hisp(record.get(56));
		object.setMax_IM_Wh_Non_Hisp(record.get(57));
		object.setCI_Min_IM_Wh_Non_Hisp(record.get(58));
		object.setCI_Max_IM_Wh_Non_Hisp(record.get(59));
		object.setIM_Bl_Non_Hisp(record.get(60));
		object.setIM_Bl_Non_Hisp_Ind(record.get(61));
		object.setMin_IM_Bl_Non_Hisp(record.get(62));
		object.setMax_IM_Bl_Non_Hisp(record.get(63));
		object.setCI_Min_IM_Bl_Non_Hisp(record.get(64));
		object.setCI_Max_IM_Bl_Non_Hisp(record.get(65));
		object.setIM_Hisp(record.get(66));
		object.setIM_Hisp_Ind(record.get(67));
		object.setMin_IM_Hisp(record.get(68));
		object.setMax_IM_Hisp(record.get(69));
		object.setCI_Min_IM_Hisp(record.get(70));
		object.setCI_Max_IM_Hisp(record.get(71));
		object.setIM_Neonatal(record.get(72));
		object.setIM_Neonatal_Ind(record.get(73));
		object.setMin_IM_Neonatal(record.get(74));
		object.setMax_IM_Neonatal(record.get(75));
		object.setCI_Min_IM_Neonatal(record.get(76));
		object.setCI_Max_IM_Neonatal(record.get(77));
		object.setIM_Postneonatal(record.get(78));
		object.setIM_Postneonatal_Ind(record.get(79));
		object.setMin_IM_Postneonatal(record.get(80));
		object.setMax_IM_Postneonatal(record.get(81));
		object.setCI_Min_IM_Postneonatal(record.get(82));
		object.setCI_Max_IM_Postneonatal(record.get(83));
		object.setBrst_Cancer(record.get(84));
		object.setBrst_Cancer_Ind(record.get(85));
		object.setMin_Brst_Cancer(record.get(86));
		object.setMax_Brst_Cancer(record.get(87));
		object.setCI_Min_Brst_Cancer(record.get(88));
		object.setCI_Max_Brst_Cancer(record.get(89));
		object.setCol_Cancer(record.get(90));
		object.setCol_Cancer_Ind(record.get(91));
		object.setMin_Col_Cancer(record.get(92));
		object.setMax_Col_Cancer(record.get(93));
		object.setCI_Min_Col_Cancer(record.get(94));
		object.setCI_Max_Col_Cancer(record.get(95));
		object.setCHD(record.get(96));
		object.setCHD_Ind(record.get(97));
		object.setMin_CHD(record.get(98));
		object.setMax_CHD(record.get(99));
		object.setCI_Min_CHD(record.get(100));
		object.setCI_Max_CHD(record.get(101));
		object.setHomicide(record.get(102));
		object.setHomicide_Ind(record.get(103));
		object.setMin_Homicide(record.get(104));
		object.setMax_Homicide(record.get(105));
		object.setCI_Min_Homicide(record.get(106));
		object.setCI_Max_Homicide(record.get(107));
		object.setLung_Cancer(record.get(108));
		object.setLung_Cancer_Ind(record.get(109));
		object.setMin_Lung_Cancer(record.get(110));
		object.setMax_Lung_Cancer(record.get(111));
		object.setCI_Min_Lung_Cancer(record.get(112));
		object.setCI_Max_Lung_Cancer(record.get(113));
		object.setMVA(record.get(114));
		object.setMVA_Ind(record.get(115));
		object.setMin_MVA(record.get(116));
		object.setMax_MVA(record.get(117));
		object.setCI_Min_MVA(record.get(118));
		object.setCI_Max_MVA(record.get(119));
		object.setStroke(record.get(120));
		object.setStroke_Ind(record.get(121));
		object.setMin_Stroke(record.get(122));
		object.setMax_Stroke(record.get(123));
		object.setCI_Min_Stroke(record.get(124));
		object.setCI_Max_Stroke(record.get(125));
		object.setSuicide(record.get(126));
		object.setSuicide_Ind(record.get(127));
		object.setMin_Suicide(record.get(128));
		object.setMax_Suicide(record.get(129));
		object.setCI_Min_Suicide(record.get(130));
		object.setCI_Max_Suicide(record.get(131));
		object.setInjury(record.get(132));
		object.setInjury_Ind(record.get(133));
		object.setMin_Injury(record.get(134));
		object.setMax_Injury(record.get(135));
		object.setCI_Min_Injury(record.get(136));
		object.setCI_Max_Injury(record.get(137));
		object.setTotal_Births(record.get(138));
		object.setTotal_Deaths(record.get(139));
		object.setMOBD_Time_Span(record.get(140));
		
		return object;
	}
}
